/* ==================================================================== *
 *
 * This file is part of Mapics Framework.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author Andrey Cherkashin <action.open@gmail.com>
 *
 * ==================================================================== */

/**
 * Main framework namespace
 */
var mapics =
{
     /**
     * Request new JavaScript file
     *
     * @param  file     CSS-File name
     * @param  callback Callback function, which will be executed after JS is
     *                  requested
     * @static m_FileCache Array of cached files
     *
     * @return bool
     */
    requestJS: function(file, callback)
    {
        // Check to see if the file cache has not been initialized
        if ( typeof mapics.requestJS.m_FileCache == 'undefined' ) {
            // It has not... perform the initilization
            mapics.requestJS.m_FileCache = {};
        }

        if( mapics.requestJS.m_FileCache[file] ) {
            if( callback != null ) {
                callback();
            }
            return false;
        }

        // Loading javascript file
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');

        script.type = 'text/javascript';
        script.src = file;

        // most browsers
        script.onload = callback;

        // IE 6 & 7
        script.onreadystatechange = function()
        {
            if (this.readyState == 'complete') {
                callback();
            }
        };

        head.appendChild(script);

        mapics.requestJS.m_FileCache[file] = true;
        return true;
    }
};